package com.nikmesoft.android.ebox.helpers;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import com.nikmesoft.android.ebox.MyApplication;
import com.nikmesoft.android.ebox.models.Item;
import com.nikmesoft.android.ebox.models.Item.Type;
import com.nikmesoft.android.ebox.models.ItemComparator;
import android.content.Context;
import android.os.Environment;

public class FileHelper {
	private static String FILTER_PHOTO = "png|jpg|gif|jpeg";
	private static String FILTER_VIDEO = "mp4|mkv|wmv|3gp";
	private static FileHelper instance;
	private String root;
	private String rootVideo;
	private String rootPhoto;

	public FileHelper(Context context) {
		super();
		root = Environment.getExternalStorageDirectory().getAbsolutePath()
				+ "/" + MyApplication.FOLDER_NAME + "/";
		rootVideo = root + "Videos/";
		rootPhoto = root + "Photos/";
		checkAppFolder();
	}

	public static void initInstance(Context context) {
		if (instance == null) {
			// Create the instance
			instance = new FileHelper(context);
		}
	}

	public static FileHelper getInstance() {
		// Return the instance
		return instance;
	}

	private void checkAppFolder() {
		File file = new File(root);
		if (!file.exists()) {
			file.mkdir();
		} else {
			if (file.isFile()) {
				file.delete();
				file.mkdir();
			}
		}
		File videoFolder = new File(rootVideo);
		videoFolder.mkdir();
		File photoFolder = new File(rootPhoto);
		photoFolder.mkdir();
	}

	public void encode(File sourceLocation, File targetLocation)
			throws IOException {

		FileInputStream fis = new FileInputStream(sourceLocation);
		FileOutputStream fos = new FileOutputStream(targetLocation);
		try {
			FileEncrypter.encrypt(MyApplication.ENCRYPT_KEY,
					(int) sourceLocation.length(),fis, fos);
		} catch (Throwable e) {
			e.printStackTrace();
		}

	}

	public void decode(File sourceLocation, File targetLocation)
			throws IOException {
		FileInputStream fis = new FileInputStream(sourceLocation);
		FileOutputStream fos = new FileOutputStream(targetLocation);
		try {
			FileEncrypter.decrypt(MyApplication.ENCRYPT_KEY,
					(int) sourceLocation.length(), fis, fos);
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

	public ArrayList<Item> getAllItems(String path, Type type) {
		String tmp = type == Type.Photo ? rootPhoto : rootVideo;
		ArrayList<Item> items = new ArrayList<Item>();
		ArrayList<Item> folders = new ArrayList<Item>();
		ArrayList<Item> files = new ArrayList<Item>();
		String fullpath = tmp + "/" + path;
		File rootFolder = new File(fullpath);
		for (File file : rootFolder.listFiles()) {
			Item item = new Item();
			if (file.isDirectory()) {
				item.setType(Type.Folder);
				item.setName(file.getName());
				folders.add(item);

			} else {
				String ext = getExt(file.getName());
				if (FILTER_PHOTO.contains(ext)) {
					item.setType(Type.Photo);
					item.setName(file.getName());
					files.add(item);
				} else if (FILTER_VIDEO.contains(ext)) {
					item.setType(Type.Video);
					item.setName(file.getName());
					files.add(item);
				}
			}
		}
		Collections.sort(folders, new ItemComparator());
		Collections.sort(files, new ItemComparator());
		items.addAll(folders);
		items.addAll(files);
		return items;
	}

	private String getExt(String filename) {
		int dotposition = filename.lastIndexOf(".");
		return filename.substring(dotposition + 1, filename.length());
	}

	public boolean addFolder(String path, Type type) {
		String tmp = type == Type.Photo ? rootPhoto : rootVideo;
		String fullpath = tmp + path;
		File folder = new File(fullpath);
		if (!folder.exists()) {
			return folder.mkdir();
		} else {
			return true;
		}
	}

	public String getFullPath(String path, Type type) {
		String tmp = type == Type.Photo ? rootPhoto : rootVideo;
		return tmp + path;
	}
}
